﻿@using Orchard.Utility.Extensions
@model ContentCreator.Settings.ContentCreatorFieldSettings
<fieldset>
    <div>
        @Html.CheckBoxFor(m => m.Required)
        <label for="@Html.FieldIdFor(m => m.Required)" class="forcheckbox">@T("A content item is required")</label>
        <span class="hint">@T("Check to ensure the user is providing at least one content item.")</span>
    </div>
</fieldset>
<fieldset>
    <div>

        <label for="@Html.FieldIdFor(m => m.ContentType)">@T("Content Type")</label>

        <select id="@Html.FieldIdFor(m => m.ContentType)" name="@Html.FieldNameFor(m => m.ContentType)">
            @foreach (string contentType in Model.ContentTypes)
            {
                @Html.SelectOption(contentType, contentType.Equals(Model.ContentType, StringComparison.OrdinalIgnoreCase), contentType.CamelFriendly())
            }
        </select>
        <span class="hint">@T("Select the content type which will be created.")</span>
        @Html.ValidationMessageFor(m => m.ContentType)
    </div>
</fieldset>
<fieldset>
    <div>
        <label for="@Html.FieldIdFor(m => m.ContentElement)">@T("Content Element")</label>
        @Html.DropDownListFor(m => m.ContentElement, Model.ContentElements)

        <span class="hint">@T("Select the content element which will be rendered on the admin dashboard.")</span>
    </div>
</fieldset>

<fieldset>
    <div>
        @Html.CheckBoxFor(m => m.Multiple)
        <label for="@Html.FieldIdFor(m => m.Multiple)" class="forcheckbox">@T("Allow multiple content items")</label>
        <span class="hint">@T("Check to allow the user to select multiple content items.")</span>
    </div>
</fieldset>
<fieldset>
    <label for="@Html.FieldIdFor(m => m.Hint)">@T("Help text")</label>
    @Html.TextAreaFor(m => m.Hint, new { @class = "textMedium", rows = "5" })
    <span class="hint">@T("The help text is written under the field when authors are selecting content items.")</span>
    @Html.ValidationMessageFor(m => m.Hint)
</fieldset>
@using (Script.Foot())
{
    <script type="text/javascript">
        //<![CDATA[
        (function ($) {
            $
                (document).ready(function () {
                    var contentTypeSelectorId = "#@Html.FieldIdFor(m => m.ContentType)";
                    var elementSelectorId = "#@Html.FieldIdFor(m => m.ContentElement)";

                    var elements = $(elementSelectorId + " option").clone();
                    
                    $(contentTypeSelectorId).change(function () {
                        var selectedContentType = $(this).val();

                        $(elementSelectorId).empty();
                        
                        //Filter the elements
                        elements.filter(function (idx, el) {
                            return $(el).val().split('/')[0] === selectedContentType;
                        }).appendTo(elementSelectorId);
                    });
                    
                    $(contentTypeSelectorId).change();
                });
        })(jQuery);
        //]]>
    </script>
}
